package com.learning.spring.aop.advisor.advisor.introduction;

import com.learning.spring.aop.advisor.monitor.AopMonitor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;

/**
 * ClassName: AopIntroductionInterceptor
 * Description: 定义一个引介 Advice
 * Date: 2019/4/21 15:40 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class AopIntroductionInterceptor extends DelegatingIntroductionInterceptor implements MonitorAble {

    private ThreadLocal<Boolean> monitorStatusMap = new ThreadLocal<>();

    @Override
    public void setMonitorActive(boolean active) {
        monitorStatusMap.set(active);
    }

    @Override
    public Object invoke(MethodInvocation mi) throws Throwable {
        Object invoke;

        if (monitorStatusMap.get() != null && monitorStatusMap.get()) {
            AopMonitor.start();
            invoke = super.invoke(mi);
            AopMonitor.end();
        } else {
            invoke = super.invoke(mi);
        }
        return invoke;
    }
}